//
//  MFWFoundation.h
//  TravelGuideMdd
//
//  Created by 陈曦 on 13-5-13.
//  Copyright (c) 2013年 mafengwo.com. All rights reserved.
//

#define SCREEN_SCALE ([UIScreen mainScreen].scale)
#define IS_RETINA() (SCREEN_SCALE >= 1.0f)
#define IS_RETINA_2X() (SCREEN_SCALE == 2.0f)
#define IS_RETINA_3X() (SCREEN_SCALE == 3.0f)

#define IS_IPAD	([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
#define IS_IPAD_HD (IS_IPAD || IS_RETINA)
#define IS_SCREEN_4 ([UIScreen mainScreen].bounds.size.height == 568.0f)
#define IS_SCREEN_4_7 ([UIScreen mainScreen].bounds.size.height == 667.0f)
#define IS_SCREEN_4_7_OR_BIGGER ([UIScreen mainScreen].bounds.size.height >= 667.0f)
#define IS_SCREEN_5_5 ([UIScreen mainScreen].bounds.size.height == 736.0f)
#define IS_SCREEN_5_5_OR_BIGGER ([UIScreen mainScreen].bounds.size.height >= 736.0f)


// 不同设备差异数据 可以优化
#define LAYOUT(IPHONE,IPHONE4,IPAD,IPAD3) ({CGFloat screenScale = [UIScreen mainScreen].scale;screenScale > 1.01?(IS_IPAD ? IPAD3 : IPHONE4):(IS_IPAD ? IPAD : IPHONE);})

// 获取本地字符串
#define LString(KEY) NSLocalizedString((KEY),nil)


// 用iOS5的Json生成方法
#define JsonEncode(object) [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding]
#define JsonDecode(jsonStr) [NSJSONSerialization JSONObjectWithData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil]


// 声明const key的宏
// KEY_VAR 产生的变量名
// KEY_NAME @"就是这个东西" 值
// KEY_TYPE key的类别  标示这个key的类别  比如userdefault notification 名字随便起
// 可以参考下面Notification系列宏
//
// 如果有重复的 link 阶段会有重复符号 就会报错了 搜索重复的符号 可以去掉重复key

#ifdef DEBUG

    #define CREATE_KEY_VALUE(KEY_VAR,KEY_NAME,KEY_TYPE)         \
                static NSString* const KEY_VAR = @#KEY_NAME;    \
                NSString *KEY_NAME##_for_##KEY_TYPE = nil;  //主要就是通过这玩意判断 如果有重复的 link 阶段会有重复符号 就会报错了 搜索重复的符号 可以去掉重复key

    #define CREATE_KEY_VALUE_PUBLIC(KEY_VAR,KEY_NAME,KEY_TYPE)  \
                NSString* const KEY_VAR = @#KEY_NAME;             \
                NSString *KEY_NAME##_for_##KEY_TYPE = nil;
#else

    #define CREATE_KEY_VALUE(KEY_VAR,KEY_NAME,KEY_TYPE)         \
            static NSString* const KEY_VAR = @#KEY_NAME;    \


    #define CREATE_KEY_VALUE_PUBLIC(KEY_VAR,KEY_NAME,KEY_TYPE)  \
            NSString* const KEY_VAR = @#KEY_NAME;             \


#endif

// 创建相同名字的变量和赋值
#define CREATE_KEY(KEY,TYPE) CREATE_KEY_VALUE(KEY,KEY,TYPE)
#define CREATE_KEY_PUBLIC(KEY,TYPE) CREATE_KEY_VALUE_PUBLIC(KEY,KEY,TYPE)

// 头文件 extern 引用
#define CREATE_KEY_PUBLIC_HEADER(KEY) extern NSString* const KEY;

// 通知中心的key
#define NotificationNameHeader(NAME)  CREATE_KEY_PUBLIC_HEADER(NAME)
#define NotificationName(NAME)        CREATE_KEY_PUBLIC(NAME,NSNotificationName)
#define NotificationVarName(VAR,NAME) CREATE_KEY_VALUE_PUBLIC(VAR,NAME,NSNotificationName)

// userdefault的key
#define UserDefaultKey(NAME) CREATE_KEY(NAME,UserDefaultKey)
#define UserDefaultVarKey(VAR,NAME) CREATE_KEY_VALUE(VAR,NAME,UserDefaultKey)

// 弱引用
#define WEAKREF(ttttt) __weak __typeof__ (ttttt) w##ttttt = ttttt;
#define STRONGREF(ttttt) __strong __typeof__ (ttttt) s##ttttt = ttttt;

// 网络诊断
#define IsWifi ([Reachability reachabilityForInternetConnection].currentReachabilityStatus == ReachableViaWiFi)
#define IsWWAN ([Reachability reachabilityForInternetConnection].currentReachabilityStatus == ReachableViaWWAN)
#define IsNetworkReachable ([Reachability reachabilityForInternetConnection].currentReachabilityStatus != NotReachable)

// 单例
#define SingletonDeclarationWithClass +(instancetype)sharedInstance;
#define SingletonImplementationWithClass \
+ (instancetype)sharedInstance {\
    static id instance = nil; \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        instance = [[self alloc] init]; \
    }); \
    return instance; \
}

// 基本block
typedef void(^BaseBlock)(void);
typedef void(^BaseBlockWithId)(id obj);
typedef void(^BaseBlockWithBool)(BOOL YesOrNo);

@interface YWFoundation : NSObject
@end